package multiThread;

import java.util.concurrent.atomic.AtomicInteger;

/**
 *
 */
public class S1114按序打印 {
}
class Foo {
    /**
     * 使用atomic类来做
     */
    private AtomicInteger jobRecord = new AtomicInteger(0);

    public Foo() {}

    public void first(Runnable printFirst) throws InterruptedException {
        // printFirst.run() outputs "first".
        printFirst.run();
        // mark the first job as done, by increasing its count.
        jobRecord.incrementAndGet();
    }

    public void second(Runnable printSecond) throws InterruptedException {
        while (jobRecord.get() != 1) {
            // waiting for the first job to be done.
        }
        // printSecond.run() outputs "second".
        printSecond.run();
        // mark the second as done, by increasing its count.
        jobRecord.incrementAndGet();
    }

    public void third(Runnable printThird) throws InterruptedException {
        while (jobRecord.get() != 2) {
            // waiting for the second job to be done.
        }
        // printThird.run() outputs "third".
        printThird.run();
    }
}